package main

import (
	"github.com/julienschmidt/httprouter"
	"html/template"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
)

func testPageHandler(w http.ResponseWriter,r *http.Request,p httprouter.Params)  {
	t,_ := template.ParseFiles("./videos/upload.html")
	t.Execute(w,nil)
}


func streamHandler(w http.ResponseWriter,r *http.Request,p httprouter.Params){
	vid := p.ByName("vid-id")
	vl := VIDEO_DIR + vid
	video,err := os.Open(vl)
	log.Printf("videos path %s",vl)
	if err != nil {
		sendErrorResponse(w,http.StatusInternalServerError,"Internal error")
		return
	}
	w.Header().Set("Content-Type","video/mp4")
	http.ServeContent(w,r,"",time.Now(),video)
	defer video.Close()

	/** 上云 操作
	targetUrl := "http://aliyun/存放的目录"
	http.Redirect(w,r,targetUrl,301)
	*/
}

func uploadHandler(w http.ResponseWriter,r *http.Request,p httprouter.Params)  {
	r.Body = http.MaxBytesReader(w,r.Body,MAX_UPLOAD_SIZE)
	if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil{
		sendErrorResponse(w,http.StatusBadRequest,"File is too big")
		return
	}
	file,_ ,err := r.FormFile("file")// <form name = "file">
	if err != nil{
		log.Printf("Error when try to open file : %v",err)
		sendErrorResponse(w,http.StatusInternalServerError,"Internal Error")
		return
	}

	data,err := ioutil.ReadAll(file)
	if err != nil{
		log.Printf("Read file error : %v",err)
		sendErrorResponse(w,http.StatusInternalServerError,"Internal Error")
		return
	}

	fn := p.ByName("vid-id")
	log.Printf("file name is :%v",fn)
	err = ioutil.WriteFile(VIDEO_DIR + fn,data,0666)
	if err != nil{
		log.Printf("Write file error:%v",err)
		sendErrorResponse(w,http.StatusInternalServerError,"Internal Error")
		return
	}


	/**上传云：先将video暂存到本地，再上传到云服务器
	ossfn := "/videos/" + filename
	path := "./videos/" + filename
	bn := "/avenssi-videos2"
	ret := uploadToOss(ossfn,path,bn)
	if !ret{
		sendErrorResponse(w,http.StatusInternalServerError,"Internal error")
		return
	}
	os.Remove(path)
	*/
	w.WriteHeader(http.StatusCreated)
	io.WriteString(w,"Uploaded successful")
}
